﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsEntityRevisionUpdateDataCommand<T> : CmsEntityRevisionUpdateCommand<T>
        where T : class, IEntityObject<int>, new()
    {
        public override IQueryable<T> Execute()
        {
            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<T>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<T>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<T, int>>();

                SqlUtility<T, int>.ExecuteNonQuery(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                         "cms_sp_EntityRevisionSave",
                        new
                        {
                            EntityId = CommandParameter.EntityId,
                            EntityTypeParameter = CommandParameter.EntityTypeParameter,
                            IsLatestRevision = CommandParameter.IsLatestRevision,
                            IsLatestCompletedRevision = CommandParameter.IsLatestCompletedRevision,
                            WorkflowId = CommandParameter.WorkflowId
                        },
                        mapper);
            }

            return null;
        }
    }
}
